﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using System.ComponentModel;

namespace WPF.Controls.QuerySelector
{
   public static class Traversing
    {

       public static FrameworkElement eq(this FrameworkElement element,int index)
       {
           return (element as Panel).Children[index] as FrameworkElement;
       }

       /// <summary>
       /// instead of is
       /// </summary>
       /// <param name="element"></param>
       /// <param name="index"></param>
       /// <returns></returns>
       public static bool check<T>(this FrameworkElement element)
       {
           return element is T;
       }

        public static List<FrameworkElement> add(this FrameworkElement element, FrameworkElement expr)
        {
            List<FrameworkElement> list = new List<FrameworkElement>();
            list.Add(element);
            list.Add(expr);
            
            return list;
        }

        public static List<FrameworkElement> add(this List<FrameworkElement> list, FrameworkElement expr)
        {
            list.Add(expr);
            return list;
        }

        public static object add(this FrameworkElement element)
        {
            return (element as ContentControl).Content;
        }

        public static object Contents(this FrameworkElement element)
        {
            return (element as ContentControl).Content;
        }

       /// <summary>
       /// element is a panel
       /// </summary>
       /// <param name="element"></param>
       /// <returns></returns>
        public static List<FrameworkElement> children(this FrameworkElement element, params Type[] ancestorType)
        {
            List<FrameworkElement> list = new List<FrameworkElement>();
            if (element is Panel)
            {
                Panel panel = element as Panel;

                foreach (var item in panel.Children)
                {
                    if (ancestorType.Length > 0)
                    {
                        if (ancestorType[0].IsInstanceOfType(item))
                        {
                            list.Add(item as FrameworkElement);
                        }
                    }
                    else
                    {
                        list.add(item as FrameworkElement);
                    }

                }
            }
            return list;
        }

        public static childItem children<childItem>(this ItemsControl element, params Type[] ancestorType)
        {
            if (element.ItemsSource != null)
            {
                return (childItem)element.ItemsSource;
            }
            else if (element.Items != null)
            {
                return (childItem)element.Items.AsQueryable().GetEnumerator();
            }

            throw new Exception();
        }

       public static FrameworkElement parent(this FrameworkElement element)
       {
           return element.Parent as FrameworkElement;
       }

       public static List<FrameworkElement> parents(this FrameworkElement element, params Type[] ancestorType)
       {
           List<FrameworkElement> list = new List<FrameworkElement>();
           FrameworkElement currentElement=element;
           while (currentElement.Parent != null)
           {
               if (ancestorType.Length>0)
               {
                   if (ancestorType[0].IsInstanceOfType(currentElement.Parent))
                   {
                       list.Add(currentElement.Parent as FrameworkElement);
                   }
               }
               else
               {
                   list.Add(currentElement.Parent as FrameworkElement);
               }
               currentElement = currentElement.Parent as FrameworkElement;
           }
           return list;
       }

       public static FrameworkElement prev(this FrameworkElement element)
       {
           Panel panel=element.Parent as Panel;
           int index=panel.Children.IndexOf(element);
           return panel.Children[index - 1] as FrameworkElement;
           //return info.GetValue(element, null);

       }

       public static FrameworkElement prev(this FrameworkElement element,Type type)
       {
           Panel panel = element.Parent as Panel;
           int index = panel.Children.IndexOf(element);
           for (int i = 0; i < panel.Children.Count; i++)
           {
               if (i < index)
               {
                   if (panel.Children[i].GetType().FullName==type.FullName)
                   {
                       return panel.Children[i] as FrameworkElement;
                   }
               }
           }
           return panel.Children[index - 1] as FrameworkElement;
           //return info.GetValue(element, null);

       }

       public static FrameworkElement next(this FrameworkElement element)
       {
           Panel panel = element.Parent as Panel;
           int index = panel.Children.IndexOf(element);
           return panel.Children[index + 1] as FrameworkElement;
           //return info.GetValue(element, null);

       }

       public static List<FrameworkElement> prevAll(this FrameworkElement element)
       {
           Panel panel = element.Parent as Panel;
           int index = panel.Children.IndexOf(element);
           List<FrameworkElement> list = new List<FrameworkElement>();
           for (int i = 0; i < panel.Children.Count; i++)
           {
               if (i < index)
               {
                   list.Add(panel.Children[i] as FrameworkElement);
               }
           }
           return list;

       }

       public static List<FrameworkElement> nextAll(this FrameworkElement element)
       {
           Panel panel = element.Parent as Panel;
           int index = panel.Children.IndexOf(element);
           List<FrameworkElement> list = new List<FrameworkElement>();
           for (int i = 0; i < panel.Children.Count; i++)
           {
               if (i > index)
               {
                   list.Add(panel.Children[i] as FrameworkElement);
               }
           }
           return list;
       }

       public static List<FrameworkElement> siblings(this FrameworkElement element)
       {
           Panel panel = element.Parent as Panel;

           List<FrameworkElement> list = new List<FrameworkElement>();
           for (int i = 0; i < panel.Children.Count; i++)
           {
               list.Add(panel.Children[i] as FrameworkElement);
           }
           return list;
       }

       //Chaining

       public static FrameworkElement beginQuery(this FrameworkElement element)
       {
           Application.Current.Properties["QuerySelectorElement"] = element;

           return element;
       }

       public static FrameworkElement endQuery(this FrameworkElement element)
       {
           Application.Current.Properties["QuerySelectorElement"] = null;

           return element;
       }

       /// <summary>
       /// please First Call beginQuery Method,if end with andSelf metohd,plase call endQuery Method
       /// </summary>
       /// <param name="element"></param>
       /// <returns></returns>
       public static List<FrameworkElement> andSelf(this FrameworkElement element)
       {
           List<FrameworkElement> list = new List<FrameworkElement>();
           list.Add(element);
           list.Add(Application.Current.Properties["QuerySelectorElement"] as FrameworkElement);
           return list;
       }

       /// <summary>
       /// please First Call beginQuery Method,if end with end metohd,plase call endQuery Method
       /// </summary>
       /// <param name="element"></param>
       /// <returns></returns>
       public static FrameworkElement end(this FrameworkElement element)
       {
          
           return Application.Current.Properties["QuerySelectorElement"] as FrameworkElement;
       }

       /// <summary>
       ///  please First Call beginQuery Method
       /// </summary>
       /// <param name="list"></param>
       /// <returns></returns>
       public static List<FrameworkElement> andSelf(this List<FrameworkElement> list)
       {
           list.Add(Application.Current.Properties["QuerySelectorElement"] as FrameworkElement);
           return list;
       }



       public static double width(this FrameworkElement element)
       {
           return element.Width;
       }

       public static FrameworkElement width(this FrameworkElement element,double width)
       {
           element.Width = width;
           return element;
       }

       public static double height(this FrameworkElement element)
       {
           return element.Height;
       }

       public static FrameworkElement height(this FrameworkElement element, double height)
       {
           element.Height = height;
           return element;
       }
    }
}
